package com.example.petshopping.ui.category;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.ui.category.adapter.CategoryAdapter;
import com.example.petshopping.ui.category.adapter.SubCategoryAdapter;
import com.example.petshopping.ui.category.model.Category;
import com.example.petshopping.ui.category.model.SubCategory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CategoryActivity extends AppCompatActivity implements CategoryAdapter.OnCategoryClickListener, SubCategoryAdapter.OnSubCategoryClickListener {

    private RecyclerView rvCategories;
    private RecyclerView rvSubCategories;
    private CategoryAdapter categoryAdapter;
    private SubCategoryAdapter subCategoryAdapter;
    
    // 分类数据
    private List<Category> categories = new ArrayList<>();
    // 子分类数据映射表
    private Map<Integer, List<SubCategory>> subCategoriesMap = new HashMap<>();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        
        initViews();
        initData();
        setupRecyclerViews();
        setupBottomNavigation();
    }
    
    private void initViews() {
        rvCategories = findViewById(R.id.rv_categories);
        rvSubCategories = findViewById(R.id.rv_subcategories);
    }
    
    private void setupBottomNavigation() {
        // 获取底部导航栏
        com.google.android.material.bottomnavigation.BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
        
        // 设置当前选中项为分类
        bottomNav.setSelectedItemId(R.id.navigation_category);
        
        // 设置点击监听器
        bottomNav.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    // 跳转到首页
                    startActivity(new Intent(this, com.example.petshopping.ui.main.MainActivity.class));
                    // 禁用页面切换动画
                    overridePendingTransition(0, 0);
                    finish();
                    return true;
                case R.id.navigation_category:
                    // 当前已在分类页
                    return true;
                case R.id.navigation_message:
                    // 跳转到消息页面
                    startActivity(new Intent(this, com.example.petshopping.ui.message.MessageActivity.class));
                    // 禁用页面切换动画
                    overridePendingTransition(0, 0);
                    finish();
                    return true;
                case R.id.navigation_profile:
                    // 跳转到我的页面
                    startActivity(new Intent(this, com.example.petshopping.ui.profile.ProfileActivity.class));
                    // 禁用页面切换动画
                    overridePendingTransition(0, 0);
                    finish();
                    return true;
            }
            return false;
        });
        
        // 设置中间按钮点击事件
        findViewById(R.id.center_button).setOnClickListener(v -> {
            Toast.makeText(this, "此功能即将上线", Toast.LENGTH_SHORT).show();
        });
    }
    
    private void initData() {
        // 初始化分类数据
        categories.add(new Category(0, "猫咪"));
        categories.add(new Category(1, "狗狗"));
        categories.add(new Category(2, "爬宠"));
        categories.add(new Category(3, "小宠"));
        categories.add(new Category(4, "鹦鹉"));
        categories.add(new Category(5, "鸟类"));
        categories.add(new Category(6, "昆虫"));
        categories.add(new Category(7, "大型宠物"));
        categories.add(new Category(8, "变异宠物"));
        
        // 初始化猫咪的子分类
        List<SubCategory> catSubCategories = new ArrayList<>(Arrays.asList(
            new SubCategory(0, "蓝白"),
            new SubCategory(1, "蓝猫"),
            new SubCategory(2, "金渐层"),
            new SubCategory(3, "银渐层"),
            new SubCategory(4, "纯白猫"),
            new SubCategory(5, "虎斑"),
            new SubCategory(6, "加白"),
            new SubCategory(7, "橘猫"),
            new SubCategory(8, "狸花猫"),
            new SubCategory(9, "奶牛猫"),
            new SubCategory(10, "三花猫"),
            new SubCategory(11, "黑猫"),
            new SubCategory(12, "布偶"),
            new SubCategory(13, "缅因"),
            new SubCategory(14, "波斯猫"),
            new SubCategory(15, "金吉拉"),
            new SubCategory(16, "拿破仑"),
            new SubCategory(17, "无毛猫"),
            new SubCategory(18, "德文卷毛"),
            new SubCategory(19, "豹猫"),
            new SubCategory(20, "美国卷耳猫"),
            new SubCategory(21, "矮脚猫"),
            new SubCategory(22, "暹罗猫"),
            new SubCategory(23, "挪威森林猫")
        ));
        subCategoriesMap.put(0, catSubCategories);
        
        // 初始化狗狗的子分类
        List<SubCategory> dogSubCategories = new ArrayList<>(Arrays.asList(
            new SubCategory(0, "泰迪"),
            new SubCategory(1, "金毛"),
            new SubCategory(2, "哈士奇"),
            new SubCategory(3, "萨摩耶"),
            new SubCategory(4, "柯基"),
            new SubCategory(5, "比熊"),
            new SubCategory(6, "博美"),
            new SubCategory(7, "边牧"),
            new SubCategory(8, "拉布拉多"),
            new SubCategory(9, "贵宾犬"),
            new SubCategory(10, "吉娃娃"),
            new SubCategory(11, "德牧"),
            new SubCategory(12, "阿拉斯加")
        ));
        subCategoriesMap.put(1, dogSubCategories);
        
        // 其他分类可以根据需要添加
        for (int i = 2; i < categories.size(); i++) {
            List<SubCategory> dummySubCategories = new ArrayList<>();
            for (int j = 0; j < 5; j++) {
                dummySubCategories.add(new SubCategory(j, categories.get(i).getName() + "品种" + (j + 1)));
            }
            subCategoriesMap.put(i, dummySubCategories);
        }
    }
    
    private void setupRecyclerViews() {
        // 设置分类列表
        categoryAdapter = new CategoryAdapter(categories, this);
        rvCategories.setLayoutManager(new LinearLayoutManager(this));
        rvCategories.setAdapter(categoryAdapter);
        
        // 设置子分类网格
        subCategoryAdapter = new SubCategoryAdapter(subCategoriesMap.get(0), this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        rvSubCategories.setLayoutManager(gridLayoutManager);
        rvSubCategories.setAdapter(subCategoryAdapter);
        
        // 默认选中第一个分类
        categoryAdapter.setSelectedPosition(0);
    }
    
    @Override
    public void onCategoryClick(int position) {
        // 更新子分类列表
        List<SubCategory> subCategories = subCategoriesMap.get(position);
        if (subCategories != null) {
            subCategoryAdapter.updateData(subCategories);
        }
    }
    
    @Override
    public void onSubCategoryClick(SubCategory subCategory) {
        // 子分类点击事件
        Toast.makeText(this, "选择了: " + subCategory.getName(), Toast.LENGTH_SHORT).show();
        // 这里可以跳转到具体的商品列表页面
    }
} 